const util = require('../../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    namelist: {},

    inputShowed: false,
    inputVal: "",


    result: {},
    noResult: false
  },


  getname: function() {

    var that = this
    wx.request({
      url: 'http://192.168.3.2/cloudwarehouse/settings/inbound/enter/getname.php',
      data: {
        openid: util.getOpenid()
      },
      success(res) {
        console.log(res.data)
        that.setData({
          namelist: res.data
        })
      },
    })
  },


  showInput: function() {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function() {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function() {
    this.setData({
      inputVal: ""
    });
  },

  inputTyping: function(e) {
    var that = this
    if (that.search(this.data.namelist, e.detail.value) != '') {
      this.setData({
        result: that.search(that.data.namelist, e.detail.value),
        noResult: true
      })
    } else
      this.setData({
        noResult: false
      });
    this.setData({
      inputVal: e.detail.value
    });
  },


  tap: function(e) {
    var id = this.getId(e)
    var price = this.getPrice(e)
    var unit = this.getUnit(e)
    wx.setStorage({
      key: 'selectedCommodity',
      data: e.currentTarget.id,
    })
    wx.setStorage({
      key: 'selectedId',
      data: id,
    })
    wx.setStorage({
      key: 'selectedPrice',
      data: price,
    })
    wx.setStorage({
      key: 'selectedUnit',
      data: unit,
    })
    wx.navigateBack({
      delta: 1
    })
  },

  getId: function(e) {
    for (var i = 0; i < this.data.namelist.length; i++) {
      if (e.currentTarget.id == this.data.namelist[i].commodityName)
        return this.data.namelist[i].commodityId;
    }
  },

  getPrice :function(e){
    for (var i = 0; i < this.data.namelist.length; i++) {
      if (e.currentTarget.id == this.data.namelist[i].commodityName)
        return this.data.namelist[i].commodityCost;
    }
  },

  getUnit: function (e) {
    for (var i = 0; i < this.data.namelist.length; i++) {
      if (e.currentTarget.id == this.data.namelist[i].commodityName)
        return this.data.namelist[i].commodityUnit_conversion;
    }
  },

  search: function(list, key) {
    var len = list.length;
    var arr = [];
    var reg = new RegExp(key);
    for (var i = 0; i < len; i++) {
      //如果字符串中不包含目标字符会返回-1
      if (list[i].commodityName.toLowerCase().match(key)) {
        arr.push(list[i]);
      }
    }
    this.setData({
      result: arr
    })
    return arr;
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getname()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})